//
//  MainViewController.m
//  看杂志
//
//  Created by mac22 on 14/12/13.
//  Copyright (c) 2014年 . All rights reserved.
//

#import "MainViewController.h"
#import "ZCJTabBar.h"
#import "MyNavigation.h"
#import "VISTAInfomationVC.h"
#import "VISTAMagazineVC.h"
#import "VISTARespectableVC.h"
#import "VISTAPictureVC.h"

@interface MainViewController ()<ZCJTabBarDelegate>
@property (nonatomic, weak) ZCJTabBar *customTabBar;
@end

@implementation MainViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    //[self createTabBarItem];
    
    [self setupTabbar];
    
    [self createViewControllers];
}
/**
 *  监听tabbar按钮的改变
 *  @param from   原来选中的位置
 *  @param to     最新选中的位置
 */
- (void)tabBar:(ZCJTabBar *)tabBar didSelectedButtonFrom:(int)from to:(int)to
{
    self.selectedIndex = to;
}


- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    // 删除系统自动生成的UITabBarButton
    for (UIView *child in self.tabBar.subviews) {
        if ([child isKindOfClass:[UIControl class]]) {
            [child removeFromSuperview];
        }
    }
    
}
- (void)setupTabbar
{
    ZCJTabBar *customTabBar = [[ZCJTabBar alloc] init];
    customTabBar.frame = self.tabBar.bounds;
    customTabBar.delegate = self;
    [self.tabBar addSubview:customTabBar];
    self.customTabBar = customTabBar;
}
- (void)createViewControllers
{
   

    
    VISTAInfomationVC *infoVC = [[VISTAInfomationVC alloc]init];
    
    [self setupChildViewController:infoVC title:@"" imageName:@"资讯_1.png" selectedImageName:@"资讯_2.png"];
    
    VISTAMagazineVC *magaVC = [[VISTAMagazineVC alloc]init];
     [self setupChildViewController:magaVC title:@"" imageName:@"杂志_1.png" selectedImageName:@"杂志_2.png"];
    
    VISTARespectableVC *respVC = [[VISTARespectableVC alloc]init];
    [self setupChildViewController:respVC title:@"" imageName:@"微言_1.png" selectedImageName:@"微言_2.png"];
    
    VISTAPictureVC *pictVC = [[VISTAPictureVC alloc]init];
    [self setupChildViewController:pictVC title:@"" imageName:@"酷图_1.png" selectedImageName:@"酷图_2.png"];
   
    
}
/**
 *  初始化一个子控制器
 *
 *  @param childVc           需要初始化的子控制器
 *  @param title             标题
 *  @param imageName         图标
 *  @param selectedImageName 选中的图标
 */
- (void)setupChildViewController:(UIViewController *)childVc title:(NSString *)title imageName:(NSString *)imageName selectedImageName:(NSString *)selectedImageName
{
    // 1.设置控制器的属性
    childVc.title = title;
    // 设置图标
    childVc.tabBarItem.image = [UIImage imageNamed:imageName];
    // 设置选中的图标
    UIImage *selectedImage = [UIImage imageNamed:selectedImageName];
    
    childVc.tabBarItem.selectedImage = selectedImage;
    
    
    // 2.包装一个导航控制器
    MyNavigation *nav = [[MyNavigation alloc] initWithRootViewController:childVc];
    [self addChildViewController:nav];
    
    // 3.添加tabbar内部的按钮
    [self.customTabBar addTabBarButtonWithItem:childVc.tabBarItem];
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
